package com.lvmama.rhino.service.impl;

import com.lvmama.rhino.pojo.screen.OrderInfoData;
import com.lvmama.rhino.pojo.screen.Product;
import com.lvmama.rhino.service.ScreenService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisCluster;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2017/6/19.
 */
@Service("screenService")
public class ScreenServiceImpl implements ScreenService {

    @Autowired
    private JedisCluster jedisCluster;

    /**
     * redis中热门景区数据
     * @return
     */
    @Override
    public List<Product> getProductList() {
        List<String> dataList = jedisCluster.lrange("rhino_productList",0,-1);
        List<Product> list = new ArrayList<Product>();
        for(String str:dataList){
            JSONArray json = JSONArray.fromObject(str);
            if(json.size()!=0){
                for (int i=0;i<json.size();i++){
                    JSONArray childJson = JSONArray.fromObject(json.get(i));
                    Product product = new Product();
                    product.setProductId(childJson.get(0).toString());
                    String productName = childJson.get(1).toString();
                    if("".equals(productName)){
                        continue;
                    }
                    product.setProductName(productName);
                    product.setViews(Long.valueOf(childJson.get(2).toString()));
                    list.add(product);
                }
            }
        }
        Collections.sort(list, new Comparator<Product>() {
            @Override
            public int compare(Product o1, Product o2) {
                long view1 = o1.getViews().longValue();
                long view2 = o2.getViews().longValue();
                if(view1<view2)
                    return 1;
                else if(view1>view2)
                    return -1;
                else
                    return 0;
            }
        });
        int count = list.size()>40?40:list.size();
        list = new ArrayList<Product>(list.subList(0,count));
        return list;
    }

    /**
     * redis中订单数据
     *
     * @return
     */
    @Override
    public List<OrderInfoData> getOrderList() {
        SimpleDateFormat sdfJson = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<String> dataList = jedisCluster.lrange("rhino_orderList",0,-1);
        List<OrderInfoData> list = new ArrayList<OrderInfoData>();
        for(String str:dataList){
            JSONArray json = JSONArray.fromObject(str);
            if(json.size()!=0){
                OrderInfoData orderInfoData = new OrderInfoData();
                orderInfoData.setProvince(json.get(0).toString());
                orderInfoData.setOperator(json.get(1).toString());
                orderInfoData.setAmount((Double) json.get(2));
                orderInfoData.setPayCount(Long.valueOf(json.get(3).toString()));
                orderInfoData.setCount(Long.valueOf(json.get(4).toString()));
                orderInfoData.setPay((boolean)json.get(5));
                try {
                    Date orderDate = sdfJson.parse(json.get(6).toString());
                    orderInfoData.setOrderDate(orderDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                list.add(orderInfoData);
            }
        }
        return list;
    }
}
